using System;
using System.IO;
using System.Linq;

using mylibrary;

namespace FileSynchronizer.DataAccess
{
    public class SyncDatabaseHelper
    {
        #region Private variables
        //---------------------------------------------------------------------------------------------------------------------------------------
        private string TheDataBase = "FileSynchronizerDB.sdf";
        private FileSynchronizerDB db;
        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion

        #region Constructors
        //---------------------------------------------------------------------------------------------------------------------------------------
        public SyncDatabaseHelper()
        {
            db = null;
        }
        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion

        #region Public functions

        //---------------------------------------------------------------------------------------------------------------------------------------
        // Create ConnectionString and copy the database to Local Application Path if it did not exist.
        //---------------------------------------------------------------------------------------------------------------------------------------
        public string DatabaseConnectionString
        {
            get
            {
                string TheDefaultDataBasePath = Path.Combine(ApplicationAttributes.ProgramPath, TheDataBase);
                string TheDatabasePath = Path.Combine(ApplicationAttributes.LocalApplicationDataPath, TheDataBase);
                String theResult = "";
                if (!File.Exists(TheDatabasePath))
                {
                    if (File.Exists(TheDefaultDataBasePath))
                    {
                        File.Copy(TheDefaultDataBasePath, TheDatabasePath);
                        theResult = TheDatabasePath;
                    }
                }
                else
                {
                    theResult = TheDatabasePath;
                }
                return theResult;
            }
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        // Create LINQ Database Connection
        //---------------------------------------------------------------------------------------------------------------------------------------
        public FileSynchronizerDB Database
        {
            get
            {
                if (db == null)
                {
                    string myConnectionString = this.DatabaseConnectionString;
                    if (myConnectionString.Length > 0)
                    {
                        db = new FileSynchronizerDB(myConnectionString);
                    }
                }
                return db;
            }
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        // Return all the Folderpairs
        //---------------------------------------------------------------------------------------------------------------------------------------
        public IQueryable<FolderPair> AllFolderPairs()
        {
            if (Database != null)
            {
                var myFolderPairs = from f in Database.FolderPairs select f;
                return myFolderPairs;
            }
            else
            {
                return null;
            }      
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        // Store a new Folderpair
        //---------------------------------------------------------------------------------------------------------------------------------------
        public FolderPair NewFolderPair(String TheLeftFolder, string TheRightFolder, bool TheAutoSynchronizeSetting)
        {
            FolderPair myNewFolderpair = null;

            if (Database != null)
            {
                myNewFolderpair = new FolderPair();

                myNewFolderpair.ID = Guid.NewGuid();
                myNewFolderpair.LeftFolder = TheLeftFolder;
                myNewFolderpair.LeftIdentifier = Guid.NewGuid();
                myNewFolderpair.RightFolder = TheRightFolder;
                myNewFolderpair.RightIdentifier = Guid.NewGuid();
                myNewFolderpair.AutoSynchronize = TheAutoSynchronizeSetting;

                Database.FolderPairs.InsertOnSubmit(myNewFolderpair);
                Database.SubmitChanges();
            }
            return myNewFolderpair;
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        // Delete an existing Folderpair
        //---------------------------------------------------------------------------------------------------------------------------------------
        public bool DeleteFolderPair(FolderPair TheFolderPair)
        {
            bool OperationResult = false;

            if (Database != null)
            {
                var AllFolderPairLogs = TheFolderPair.FolderPairLogs;
                foreach (FolderPairLog theFolderPairLog in AllFolderPairLogs)
                {
                    Database.FolderPairLogs.DeleteOnSubmit(theFolderPairLog);
                }


                Database.FolderPairs.DeleteOnSubmit(TheFolderPair);
                Database.SubmitChanges();
                OperationResult = true;
            }

            return OperationResult;
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        // Store a new FolderPairLog
        //---------------------------------------------------------------------------------------------------------------------------------------
        public bool AddFolderPairLog(FolderPairLog TheFolderPairLog)
        {
            bool OperationResult = false;

            if (Database != null)
            {
                if (TheFolderPairLog.ID.Equals(Guid.Empty))
                {
                    TheFolderPairLog.ID = Guid.NewGuid();
                }
                Database.FolderPairLogs.InsertOnSubmit(TheFolderPairLog);
                Database.SubmitChanges();
                OperationResult = true;
            }
            return OperationResult;
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        // Return all FolderpairLogs for a specific Folderpair
        //---------------------------------------------------------------------------------------------------------------------------------------
        public IQueryable<FolderPairLog> AllFolderPairLogs(FolderPair TheFolderPair)
        {
            if (TheFolderPair != null)
            {
                if (Database != null)
                {
                    var myFolderPairLogs = from f in Database.FolderPairLogs 
                                        where f.FolderPair_ID == TheFolderPair.ID select f;
                    return myFolderPairLogs;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        // Clear all FolderpairLogs for a specific Folderpair
        //---------------------------------------------------------------------------------------------------------------------------------------
        public bool DeleteFolderPairLogs(FolderPair TheFolderPair)
        {
            bool OperationResult = false;

            if (Database != null)
            {
                var myFolderPairLogs = from f in Database.FolderPairLogs 
                                        where f.FolderPair_ID == TheFolderPair.ID select f;
                foreach (FolderPairLog theFolderPairLog in myFolderPairLogs)
                {
                    Database.FolderPairLogs.DeleteOnSubmit(theFolderPairLog);
                }

                Database.SubmitChanges();
                OperationResult = true;
            }

            return OperationResult;
        }
        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion
    }
}
